package com.abe.bathsystem.widget.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.abe.bathsystem.utils.ConstantBath;
import com.abe.bathsystem.widget.OnOperateListener;
import com.abe.libcore.core.screen.RelayoutTool;
import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.EmptyUtils;


public class BaseDialog extends Dialog implements DialogInterface.OnDismissListener {

    private OnOperateListener operateListener;

    BaseDialog(@NonNull Context context, OnOperateListener operateListener) {
        this(context);
        this.operateListener = operateListener;
    }

    BaseDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = View.inflate(getContext(), layoutResID, null);
        setOnDismissListener(this);
        this.setContentView(view);
    }

    @Override
    public void setContentView(@NonNull View view) {
        RelayoutTool.relayoutViewHierarchy(view, getScale());
        super.setContentView(view);
    }

    private float getScale() {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        float width = displayMetrics.widthPixels;
        return width / getDesignWidth();
    }

    private float getDesignWidth() {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        float width = displayMetrics.widthPixels;
        int DesignWidth = CommonUtils.getMetaIntData(getContext(), ConstantBath.META_DATA_DESIGN_WIDTH);
        if (DesignWidth == 0) return width;
        return DesignWidth;
    }

    @Override
    public void show() {
        CommonUtils.w("dialog-show");
        super.show();
        operate();
        Window dialogWindow = getWindow();
        if (dialogWindow == null) return;
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.85); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (!isCallDismiss) {
            isCallDismiss = true;
            CommonUtils.w("CallDismiss");
            if (isAnimationEnd) {
                super.dismiss();
            } else {
                new BaseHandler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isAnimationEnd = true;
                        new BaseHandler(Looper.getMainLooper()).sendEmptyMessage(0x101);
                    }
                }, 500);
            }
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        CommonUtils.w("Dismiss");
    }

    class BaseHandler extends Handler {
        BaseHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            BaseDialog.super.dismiss();
        }
    }

    @Override
    public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //有按下动作时取消定时
                operate();
                break;
            case MotionEvent.ACTION_MOVE:
                //在按住滑动时取消定时
                operate();
                break;
            case MotionEvent.ACTION_UP:
                //抬起时启动定时
                operate();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private void operate() {
        if (operateListener != null) {
            operateListener.onOperate();
        }
    }

    public void showWithAnim(int style) {
        //获取Dialog所在的Window
        Window window = getWindow();
        if (EmptyUtils.isNotEmpty(window)) {
            //为Window设置动画
            window.setWindowAnimations(style);
            isAnimationEnd = true;
            isCallDismiss = false;
        }
        show();
    }

    private boolean isAnimationEnd = true;
    private boolean isCallDismiss = false;

    @Override
    public boolean isShowing() {
        return isAnimationEnd && super.isShowing();
    }
}
